#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>

int tickets = 1000;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *start_routine(void *args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        std::cout << name << " -> " << tickets-- << std::endl;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
#define NUM 5
    pthread_t t[5];

    for(int i = 0; i < NUM; ++i)
    {
        char *buffer = new char[64];
        snprintf(buffer, 64, "thread %d", i+1);
        pthread_create(&t[i], nullptr, start_routine, buffer);
    }

    while(true)
    {
        sleep(1);
        // pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        std::cout << "main pthread wakeup some subpthread" << std::endl;
    }

    for(int i = 0; i < NUM; ++i)
    {
        pthread_join(t[i], nullptr);
    }
    return 0;
}